//
//  CDGetUserInfoStep.swift
//  casdon_swiftUI
//
//  Created by casdon on 2025/4/22.
//

import Combine

struct CDGetUserLoginInfoStep: LoginStep {
    struct InputData {}

    func execute(_ input: InputData) -> AnyPublisher<AuthAPI.UserLogInInfo.Response, Error> {
        Future { promise in
            let api = AuthAPI.UserLogInInfo(body: .init())
            NetworkManager.shared.send(api) { result in
                switch result {
                    case .success(let response):
                        // 假设 response 是 APIResponse 类型，提取出 response.data
                        if let userInfo = response.data {
                            // 成功获取用户信息后，调用 promise 返回结果
                            promise(.success(userInfo))
                        } else {
                            // 如果没有数据，返回失败
                            promise(.failure(NSError(domain: "获取用户信息", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                        }
                    case .failure(let error):
                        // 网络请求失败时返回错误
                        promise(.failure(error))
                }
            }
        }
        .eraseToAnyPublisher()  // 转换为 AnyPublisher 类型
    }
}
  
